import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json',
    Authorization:
      'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYmVzdGx5ZyIsImV4cCI6MTU4NTY0NTQwOCwiaXNzIjoiYmVzdGx5Zy5jb20iLCJhdWQiOiJXZWJBcGkifQ.2q2FHKw5odrSeB5mxn2FZZyAgnb6AD5vwmhyKoA9i1g'
  }),
  withCredentials: true
};
const baseUrl = 'http://localhost:5872';
@Component({
  selector: 'app-back-end-test',
  templateUrl: './back-end-test.component.html',
  styleUrls: ['./back-end-test.component.less']
})
export class BackEndTestComponent implements OnInit {
  public jsonData: any;
  constructor(private http: HttpClient) {}
  ngOnInit(): void {}
  getAllUser() {
    this.http
      .get(baseUrl + '/api/users')
      .subscribe(data => (this.jsonData = data));
  }
  getUser() {
    this.http
      .get(baseUrl + '/api/users/123')
      .subscribe(data => (this.jsonData = data));
  }
  postUser() {
    this.http
      .post(
        baseUrl + '/api/users',
        {
          account: '0121',
          password: '123',
          email: '213'
        },
        httpOptions
      )
      .subscribe(data => (this.jsonData = data));
  }
  getAllTable() {
    this.http
      .get(baseUrl + '/api/luckytables')
      .subscribe(data => (this.jsonData = data));
  }
  getTable() {
    this.http
      .get(baseUrl + '/api/luckytables/5e82f4a8f47f8b2a9411bed9')
      .subscribe(data => (this.jsonData = data));
  }
  postTable() {
    const numberDTO = {
      title: '1',
      type: 'number',
      number: 123,
      userId: '5e83fba94a8b7b3754609f6b'
    };
    const stringDTO = {
      title: '1',
      type: 'string',
      string: ['1', '2', '3'],
      userId: '5e83fba94a8b7b3754609f6b'
    };
    this.http
      .post(baseUrl + '/api/luckytables', stringDTO, httpOptions)
      .subscribe(data => (this.jsonData = data));
  }
  deleteTable() {
    this.http
      .delete(
        baseUrl + '/api/luckytables/5e82e9c320dde84328824427',
        httpOptions
      )
      .subscribe(data => (this.jsonData = data));
  }
  login() {
    this.http
      .post(
        baseUrl + '/api/users/login',
        {
          account: 'bestlyg',
          password: '2'
        },
        httpOptions
      )
      .subscribe(data => (this.jsonData = data));
  }
  getWeatherForecast() {
    this.http
      .get(baseUrl + '/weatherforecast', httpOptions)
      .subscribe(data => (this.jsonData = data));
  }
  getAllTodoItem() {
    this.http
      .get(baseUrl + '/api/TodoItems')
      .subscribe(data => (this.jsonData = data));
  }
  getTodoItem() {
    this.http
      .get(baseUrl + '/api/TodoItems/1')
      .subscribe(data => (this.jsonData = data));
  }
  postTodoItem() {
    this.http
      .post(
        baseUrl + '/api/TodoItems',
        {
          name: 'walk dog',
          isComplete: true
        },
        httpOptions
      )
      .subscribe(data => (this.jsonData = data));
  }
  putTodoItem() {
    this.http
      .put(
        baseUrl + '/api/TodoItems/1',
        {
          ID: 1,
          name: 'feed fish',
          isComplete: true
        },
        httpOptions
      )
      .subscribe(data => (this.jsonData = data));
  }
  deleteTodoItem() {
    this.http
      .get(baseUrl + '/api/TodoItems/1', httpOptions)
      .subscribe(data => (this.jsonData = data));
  }
}
